#!/bin/bash
# 项目测试脚本

echo "=========================================="
echo "  医学影像AI分析系统 - 功能测试"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 测试结果统计
PASSED=0
FAILED=0

# 测试函数
test_api() {
    local name=$1
    local url=$2
    local method=${3:-GET}
    local data=$4
    
    echo -n "测试 $name ... "
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" "$url")
    else
        response=$(curl -s -w "\n%{http_code}" -X POST -H "Content-Type: application/json" -d "$data" "$url")
    fi
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | sed '$d')
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✅ 通过${NC} (HTTP $http_code)"
        ((PASSED++))
        return 0
    else
        echo -e "${RED}❌ 失败${NC} (HTTP $http_code)"
        echo "响应: $body" | head -2
        ((FAILED++))
        return 1
    fi
}

echo "🔍 第一部分：服务状态检查"
echo "----------------------------------------"

# 1. 检查后端服务
echo -n "检查后端服务 (8001端口) ... "
if curl -s http://localhost:8001/docs > /dev/null 2>&1; then
    echo -e "${GREEN}✅ 运行中${NC}"
    ((PASSED++))
else
    echo -e "${RED}❌ 未运行${NC}"
    ((FAILED++))
fi

# 2. 检查前端服务
echo -n "检查前端服务 (5174端口) ... "
if netstat -tlnp 2>/dev/null | grep -q :5174; then
    echo -e "${GREEN}✅ 运行中${NC}"
    ((PASSED++))
else
    echo -e "${YELLOW}⚠️  未运行 (需要手动启动)${NC}"
    echo "  提示：在新终端运行 'cd frontend && npm run dev -- --port 5174 --host 0.0.0.0'"
fi

echo ""
echo "🧪 第二部分：后端API功能测试"
echo "----------------------------------------"

# 3. 测试会话API
test_api "获取会话列表" "http://localhost:8001/api/sessions"

# 4. 测试创建会话
test_api "创建新会话" "http://localhost:8001/api/sessions" "POST" '{"model_name":"通用对话"}'

# 5. 测试患者信息API
test_api "获取患者信息" "http://localhost:8001/api/medical/patient-info"

# 6. 测试更新患者信息
test_api "更新患者信息" "http://localhost:8001/api/medical/update-patient" "POST" '{"name":"测试患者","age":"30","gender":"男","history":"无"}'

echo ""
echo "📊 第三部分：数据存储检查"
echo "----------------------------------------"

# 7. 检查数据目录
echo -n "检查数据目录 ... "
if [ -d "backend/data" ]; then
    echo -e "${GREEN}✅ 存在${NC}"
    ((PASSED++))
    echo "  - 会话文件: $(ls backend/data/sessions.json 2>/dev/null && echo '存在' || echo '不存在')"
    echo "  - 消息数量: $(ls -1 backend/data/messages/*.json 2>/dev/null | wc -l) 个"
else
    echo -e "${RED}❌ 不存在${NC}"
    ((FAILED++))
fi

echo ""
echo "🎨 第四部分：前端文件检查"
echo "----------------------------------------"

# 8. 检查前端编译文件
echo -n "检查node_modules ... "
if [ -d "frontend/node_modules" ]; then
    echo -e "${GREEN}✅ 已安装${NC}"
    ((PASSED++))
else
    echo -e "${RED}❌ 未安装${NC}"
    echo "  运行: cd frontend && npm install"
    ((FAILED++))
fi

# 9. 检查关键组件
echo -n "检查前端组件 ... "
components=(
    "frontend/src/pages/MedicalAnalysis.vue"
    "frontend/src/pages/Chat.vue"
    "frontend/src/components/MedicalPanel.vue"
)
missing=0
for comp in "${components[@]}"; do
    if [ ! -f "$comp" ]; then
        ((missing++))
    fi
done

if [ $missing -eq 0 ]; then
    echo -e "${GREEN}✅ 完整${NC}"
    ((PASSED++))
else
    echo -e "${YELLOW}⚠️  缺少 $missing 个文件${NC}"
fi

echo ""
echo "🔧 第五部分：配置文件检查"
echo "----------------------------------------"

# 10. 检查requirements.txt
echo -n "检查Python依赖配置 ... "
if [ -f "requirements.txt" ]; then
    echo -e "${GREEN}✅ 存在${NC}"
    echo "  - 包数量: $(cat requirements.txt | grep -v '^#' | grep -v '^$' | wc -l) 个"
    ((PASSED++))
else
    echo -e "${RED}❌ 不存在${NC}"
    ((FAILED++))
fi

# 11. 检查package.json
echo -n "检查前端依赖配置 ... "
if [ -f "frontend/package.json" ]; then
    echo -e "${GREEN}✅ 存在${NC}"
    ((PASSED++))
else
    echo -e "${RED}❌ 不存在${NC}"
    ((FAILED++))
fi

# 12. 检查启动脚本
echo -n "检查启动脚本 ... "
if [ -f "scripts/start_backend.sh" ] && [ -f "scripts/start_frontend.sh" ]; then
    echo -e "${GREEN}✅ 完整${NC}"
    ((PASSED++))
else
    echo -e "${YELLOW}⚠️  部分缺失${NC}"
fi

echo ""
echo "=========================================="
echo "  测试总结"
echo "=========================================="
echo -e "通过: ${GREEN}$PASSED${NC}"
echo -e "失败: ${RED}$FAILED${NC}"
echo ""

# 计算总分
total=$((PASSED + FAILED))
if [ $total -gt 0 ]; then
    score=$((PASSED * 100 / total))
    echo "总分: $score/100"
    
    if [ $score -ge 80 ]; then
        echo -e "${GREEN}🎉 项目状态：良好${NC}"
    elif [ $score -ge 60 ]; then
        echo -e "${YELLOW}⚠️  项目状态：需要改进${NC}"
    else
        echo -e "${RED}❌ 项目状态：需要修复${NC}"
    fi
fi

echo ""
echo "📝 访问地址："
echo "  - 前端界面: http://localhost:5174"
echo "  - 后端API: http://localhost:8001"
echo "  - API文档: http://localhost:8001/docs"
echo ""
echo "=========================================="

